package com.example.objectstoragejava.web;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "oss")
public class OssUpload {
    @GetMapping(value = "get-sts")
    public AssumeRoleResponse.Credentials getSts() throws Exception {
        try {
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-hangzhou",
                    "LTAI5t63S3yMFWwzKte5x4Zu",
                    "smMBqtQx3Ttr9iQeCxMYH3Q0f7jVXO"
            );
            DefaultAcsClient client = new DefaultAcsClient(profile);

            AssumeRoleRequest request = new AssumeRoleRequest();
            request.setRoleArn("acs:ram::test:role/RamTestAppReadOnly");
            request.setRoleSessionName("session-name");

            AssumeRoleResponse response = client.getAcsResponse(request);

            log.info("获取STSToken成功 {}", response.getCredentials());
            return response.getCredentials();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
